$(function() {
	admin.product.init();
});
var saveFlag = false;
Namespace('admin.product', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
				/*var row = $('#datagrid').datagrid('getSelected');
				admin.product.edit(row.id);*/
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	toAdd : function(shopId) {
		top.addTab('#product'+shopId,'添加产品', contextPath+'/mall/product/add/'+shopId);
	},
	toEdit:function(id) {
		if(id==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		top.addTab(id,'产品编辑', contextPath+'/mall/product/update/'+id);
	},
	list : function(shopId,shopName) {
		top.addTab('#productlist'+shopId,shopName+'的产品', contextPath+'/mall/product/list/'+shopId);
	},
	netMsgChange:function(id,maxCount) {
		var inputLength = $('#'+id).val().length;
		if (inputLength == 0) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + maxCount + '个字');
			saveFlag = false;
		} else if (inputLength > 0 && inputLength <= maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + (maxCount - inputLength) + '个字');
			saveFlag = false;
		} else if (inputLength > maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，已经输入超过<strong class="cred">' + (inputLength - maxCount) + '</strong>个字');
			saveFlag = true;
		}
	},
	addImg:function(id){
		getDialog('query-dialog').dialog({
			href:contextPath+'/mall/product/imgUpload',
			title: '选择文件',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'上传',
				handler:function(){
					uploaderStart(id);
				}
			}]
		});
		
	},
	setDiscoverRecommend: function (id,recommendValue) {
		var url = contextPath + '/mall/product/updateRecommend';
		$.postJson(url, {'id': id,'discoverRecommend':recommendValue}, function (result) {
			if (result.code == 1) {
				top.showInfo('操作成功');
				var data = $('#query-form').form('jsonObject');
				$('#datagrid').datagrid('load', data);
			} else {
				top.showInfo(result.msg);
			}
		});
	}
});